package com.ck.server.controller.test.login.rsa.business;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ck.common.bean.McApiResp;
import com.ck.common.entity.SysUser;
import com.ck.common.utils.page.TableDataInfo;
import com.ck.common.vo.ReqVo;
import com.ck.server.controller.BaseController;
import com.ck.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author ck
 * @date 2024/1/23 9:41
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = {"用户管理接口"})
public class UserController extends BaseController {

    @Autowired
    SysUserService sysUserService;

    @ApiOperation(value = "查询用户列表")
    @PostMapping("/list")
    public McApiResp list(@RequestBody ReqVo reqVo) {

        HashMap<String, Object> respMap = new HashMap<>();
        JSONObject jsonObject = JSON.parseObject(reqVo.getD());
        String id = jsonObject.getString("id");

        if (id!=null){
            SysUser user = sysUserService.getUserById(id);
            Assert.notNull(user,"查询失败,用户不存在");
            respMap.put("data",user);
        } else {
            String userName = jsonObject.getString("userName");
            String phone = jsonObject.getString("phone");
            String startDate = jsonObject.getString("startDate");
            String endDate = jsonObject.getString("endDate");
            startPage();
            List<SysUser> list = sysUserService.selectUserList(userName,phone,startDate,endDate);
            TableDataInfo dataTable = getDataTable(list);
            respMap.put("data",dataTable);
        }
        return McApiResp.success(respMap);
    }
}
